package cn.donting.jme.utile.savable;

import cn.hutool.core.io.FileUtil;
import com.jme3.app.SimpleApplication;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.light.AmbientLight;
import com.jme3.scene.Spatial;
import com.jme3.system.JmeContext;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class Test extends SimpleApplication {
    public static void main(String[] args) throws Exception {
        new Test().start(JmeContext.Type.OffscreenSurface);
//        TxtlJmeImporter txtlJmeImporter = new TxtlJmeImporter(null);
//        txtlJmeImporter.load(save);
    }

    @Override
    public void simpleInitApp() {
        Spatial spatial = assetManager.loadModel("tranlation/translation.j3o");

        TxtJmeExporter txtJmeExporter = new TxtJmeExporter();
        String save = null;
        try {
            save = txtJmeExporter.save(spatial);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        FileUtil.writeString(save,new File("./t.txt"), StandardCharsets.UTF_8);
        System.out.println(save);
    }

    public static class TestNode implements Savable{

        @Override
        public void write(JmeExporter ex) throws IOException {
            OutputCapsule capsule = ex.getCapsule(this);
            capsule.write(new int[]{1,2,3},"name",null);
            capsule.write(new int[][]{{1,2},{1,2},{1,2,3}},"name2",null);
            ArrayList<Savable> arrayList = new ArrayList<>();
            arrayList.add(new AmbientLight());
            arrayList.add(new AmbientLight());
            ArrayList<Savable>[] list2=new ArrayList[2];
            list2[0]=arrayList;
            list2[1]=arrayList;
            ArrayList<Savable>[][] list3=new ArrayList[2][];
            list3[0]=list2;
            list3[1]=list2;
            capsule.writeSavableArrayList(arrayList,"list",null);
            capsule.writeSavableArrayListArray(list2,"list2",null);
            capsule.writeSavableArrayListArray2D(list3,"list3",null);
        }

        @Override
        public void read(JmeImporter im) throws IOException {

        }
    }
}
